<template>
  <!-- 新增部门的弹层 -->
  <el-dialog title="新增员工" :visible="showDialog" @close="btnOff">
    <!-- 表单组件  el-form   label-width设置label的宽度   -->
    <!-- 匿名插槽 -->
    <el-form
      ref="employeeForm"
      label-width="120px"
      :rules="rules"
      :model="formData"
    >
      <el-form-item label="姓名" prop="username">
        <el-input
          v-model="formData.username"
          style="width: 90%"
          placeholder="请输入姓名"
        />
      </el-form-item>
      <el-form-item label="手机" prop="mobile">
        <el-input
          v-model="formData.mobile"
          style="width: 90%"
          placeholder="请输入手机号"
        />
      </el-form-item>
      <el-form-item label="入职时间" prop="timeOfEntry">
        <el-date-picker
          v-model="formData.timeOfEntry"
          style="width: 90%"
          placeholder="请选择日期"
        />
      </el-form-item>
      <el-form-item label="聘用形式" prop="formOfEmployment">
        <el-select
          v-model="formData.formOfEmployment"
          style="width: 90%"
          placeholder="请选择"
        >
          <!-- (只能)遍历组件的数据 -->
          <el-option
            v-for="item in EmployeeEnum.hireType"
            :key="item.id"
            :label="item.value"
            :value="item.id"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="工号" prop="workNumber">
        <el-input
          v-model="formData.workNumber"
          style="width: 90%"
          placeholder="请输入工号"
        />
      </el-form-item>
      <el-form-item label="部门" prop="departmentName">
        <el-input
          v-model="formData.departmentName"
          style="width: 90%"
          placeholder="请选择部门"
          @focus="getDepartments"
        /><el-tree
          v-if="showTree"
          class="treeData"
          :data="treeData"
          default-expand-all
          :props="{ label: 'name' }"
          @node-click="selectNode"
        /></el-form-item>
      <el-form-item label="转正时间">
        <el-date-picker
          v-model="formData.correctionTime"
          prop="correctionTime"
          style="width: 90%"
          placeholder="请选择日期"
        />
      </el-form-item>
    </el-form>
    <!-- el-dialog有专门放置底部操作栏的 插槽  具名插槽 -->
    <el-row slot="footer" type="flex" justify="center">
      <!-- 列被分为24 -->
      <el-col :span="6">
        <el-button
          type="primary"
          style="width: 100px"
          @click="btnOn"
        >确定</el-button>
        <el-button style="width: 100px" @click="btnOff">取消</el-button>
      </el-col>
    </el-row>
  </el-dialog>
</template>

<script>
import { transListToTreeData } from '@/utils'
import { getDepartmentList } from '@/api/departments'
import { addEmployee } from '@/api/employees'
import EmployeeEnum from '@/api/constant/employees'
export default {
  props: {
    showDialog: {
      type: Boolean,
      default: false
    }
  },
  data() {
    return {
      // 将枚举数据放入 data, 方便模板使用
      EmployeeEnum,
      showTree: false,
      // 部门树形组件数据
      treeData: [],
      loading: false,
      // 定义表单数据
      formData: {
        username: '',
        mobile: '',
        formOfEmployment: '',
        workNumber: '',
        departmentName: '',
        timeOfEntry: '',
        correctionTime: ''
      },
      rules: {
        username: [
          { required: true, message: '用户姓名不能为空', trigger: 'blur' },
          {
            min: 1,
            max: 4,
            message: '用户姓名为1-4位',
            trigger: 'blur'
          }
        ],
        mobile: [
          { required: true, message: '手机号不能为空', trigger: 'blur' },
          {
            pattern: /^1[3-9]\d{9}$/,
            message: '手机号格式不正确',
            trigger: 'blur'
          }
        ],
        formOfEmployment: [
          { required: true, message: '聘用形式不能为空', trigger: 'blur' }
        ],
        workNumber: [
          { required: true, message: '工号不能为空', trigger: 'blur' }
        ],
        departmentName: [
          { required: true, message: '部门不能为空', trigger: 'change' }
        ],
        timeOfEntry: [{ required: true, message: '入职时间', trigger: 'blur' }]
      }
    }
  },
  methods: {
    // 点击部门后填充内容到框内
    // node-click 事件可以返回三个参数
    selectNode(data, b, c) {
      this.formData.departmentName = data.name
      this.showTree = false
    },
    // 获取部门数据
    async getDepartments() {
      this.showTree = true
      // 获取栏目列表并且显示出来
      // 获取部门列表(扁平化)
      const { depts } = await getDepartmentList()
      // 将数据转换为树形嵌套结构
      this.treeData = transListToTreeData(depts, '')
    },

    // 确定
    async btnOn() {
      try {
        // 校验表单,validate用于rule验证规则
        await this.$refs.employeeForm.validate()
        // 发送添加请求
        await addEmployee(this.formData) // 新增
        this.$message.success('添加成功')
        // 刷新页面
        // 可以使用$parent实现$emit的功能
        this.$parent.toGetEmployeesList()
        // 关闭输入框
        this.$parent.showDialog = false
      } catch (error) {
        console.log('员工', error)
      }
    },
    // 取消
    async btnOff() {
      // 为了防止获取了数据后除了name ,code,manager,introduce 外的其他数据没有清除
      // 所有收到清除所有数据
      this.formData = {
        username: '',
        mobile: '',
        formOfEmployment: '',
        workNumber: '',
        departmentName: '',
        timeOfEntry: '',
        correctionTime: ''
      }
      // 重置记录数据
      await this.$refs.employeeForm.resetFields()
      // 关闭输入框
      // this.$emit('update:showDialog', false)
      // 可以使用$parent实现$emit的功能
      this.$parent.showDialog = false
    }
  }
}
</script>

<style>
.el-dialog__body {
  padding: 40px 120px;
}
.treeData {
  position: absolute;
  z-index: 9;
  border: 1px solid #ddd;
  width: 300px;
  height: 200px;
  overflow-y: scroll;
}
</style>
